using Cysharp.Threading.Tasks;
using UnityEngine;

namespace Game {
	public abstract class InventoryItemPresenter<T> : InventoryItemPresenter where T : BaseInventoryItem {
		public abstract void Setup(T item);
	}

	public abstract class InventoryItemPresenter : MonoBehaviour {
		public virtual void Kill() {
			//TODO: return to pool or something
			Destroy(gameObject);
		}

		public async UniTask MoveToPointAndKill(Vector3 point, float flyTime) {
			var moveVector = point - transform.position;
			var moveSpeed = moveVector / flyTime;
			var curTime = 0f;
			while ( curTime < flyTime ) {
				await UniTask.Yield(PlayerLoopTiming.Update);
				if ( transform ) {
					transform.position += moveSpeed * Time.deltaTime;
				}				
				curTime += Time.deltaTime;
			}
			Kill();
		}

		public async UniTask MoveToTransformAndKill(Transform r, float flyTime) {
			var startPos = transform.position;
			var curTime = 0f;
			while ( curTime < flyTime ) {
				await UniTask.Yield(PlayerLoopTiming.Update);
				var percent = Mathf.InverseLerp(0, flyTime, curTime);
				if ( this && r ) {
					transform.position = Vector3.Lerp(startPos, r.position, percent);
				}
				curTime += Time.deltaTime;
			}
			Kill();
		}
	}
}